/**
 * 
 */
package com.orcwar.engine.units;

import com.orcwar.engine.OWMap;
import com.orcwar.engine.OWPlayer;
import com.orcwar.engine.OWTile;
import com.orcwar.engine.OWUnit;
import com.orcwar.engine.tile.OWAttackTile;
import com.orcwar.enums.OWAttackTileTypeEnum;
import com.orcwar.enums.SpriteEnum;

/**
 * @author Callil
 *
 */
public class OWFleauteur extends OWUnit {

	/**
	 * Constructor
	 * @param id the id
	 */
	public OWFleauteur(int id, OWPlayer player) {
		super(id, player, SpriteEnum.UNIT_FLEAUTEUR);
		
		//The fleauteur can attack 1 time
		setAttackPoints(1);
		setMaxAttackPoints(1);
		//and move up to 2 tiles
		setMaxMovePoints(2);
		setMovePoints(2);
	}

	@Override
	public OWAttackTile[] getGlobalMandatoryAvailableTiles(OWMap map, OWTile fromTile) {
		//FOr the FLEAUTEUR : all tiles around him must be empty so that he can attack
		int x;
		int y;
		if (fromTile != null) {
			x = fromTile.getX();
			y = fromTile.getY();
		} else {
			x= this.getTile().getX();
			y = this.getTile().getY();
		}
		
		OWAttackTile[] mandatoryTiles = new OWAttackTile[]{
				OWAttackTile.create(map.getTile(x-1, y-1), OWAttackTileTypeEnum.MANDATORY_NOT_UNIT),
				OWAttackTile.create(map.getTile(x-1, y), OWAttackTileTypeEnum.MANDATORY_NOT_UNIT), 
				OWAttackTile.create(map.getTile(x-1, y+1), OWAttackTileTypeEnum.MANDATORY_NOT_UNIT),
				OWAttackTile.create(map.getTile(x, y-1), OWAttackTileTypeEnum.MANDATORY_NOT_UNIT), 
				OWAttackTile.create(map.getTile(x, y+1), OWAttackTileTypeEnum.MANDATORY_NOT_UNIT),
				OWAttackTile.create(map.getTile(x+1, y-1), OWAttackTileTypeEnum.MANDATORY_NOT_UNIT),
				OWAttackTile.create(map.getTile(x+1, y), OWAttackTileTypeEnum.MANDATORY_NOT_UNIT),
				OWAttackTile.create(map.getTile(x+1, y+1), OWAttackTileTypeEnum.MANDATORY_NOT_UNIT)
		};
		
		return mandatoryTiles;
	}
	
	@Override
	public boolean isRangeOk(int srcX, int srcY, int targetX, int targetY) {
		//The fleauteur can attack all around him at only 2 tile of distance
		//and cannot attack at 1 tile
		// _ _ _ _ _
		//|x|x|x|x|x|
		//|x| | | |x|
		//|x| |H| |x|
		//|x| | | |x|
		//|x|x|x|x|x|
		
		boolean xAt2 = Math.abs(srcX - targetX) == 2;
		boolean xAt2OrLess = Math.abs(srcX - targetX) <= 2;
		boolean yAt2 = Math.abs(srcY - targetY) == 2;
		boolean yAt2OrLess = Math.abs(srcY - targetY) <= 2;
		
		return (xAt2 && yAt2OrLess) || (yAt2 && xAt2OrLess);
	}

	@Override
	public OWAttackTile[][] getAttackTiles(OWMap map, OWTile fromTile) {
		//The fleauteur can attack all around him at only 2 tile of distance
		//and cannot attack at 1 tile
		// _ _ _ _ _
		//|x|x|x|x|x|
		//|x| | | |x|
		//|x| |H| |x|
		//|x| | | |x|
		//|x|x|x|x|x|
		int x;
		int y;
		if (fromTile != null) {
			x = fromTile.getX();
			y = fromTile.getY();
		} else {
			x= this.getTile().getX();
			y = this.getTile().getY();
		}
		OWAttackTile[][] attackTiles = new OWAttackTile[][]{ 
				//Upper left
				new OWAttackTile[] {OWAttackTile.create(map.getTile(x-2, y-2), 
						OWAttackTile.create(map.getTile(x-2, y-1), OWAttackTileTypeEnum.ATTACKABLE), OWAttackTile.create(map.getTile(x-1, y-2), OWAttackTileTypeEnum.ATTACKABLE))},
				
				//Upper middle
				new OWAttackTile[] {OWAttackTile.create(map.getTile(x-1, y-2), 
						OWAttackTile.create(map.getTile(x-2, y-2), OWAttackTileTypeEnum.ATTACKABLE), OWAttackTile.create(map.getTile(x, y-2), OWAttackTileTypeEnum.ATTACKABLE))},
				new OWAttackTile[] {OWAttackTile.create(map.getTile(x, y-2), 
						OWAttackTile.create(map.getTile(x-1, y-2), OWAttackTileTypeEnum.ATTACKABLE), OWAttackTile.create(map.getTile(x+1, y-2), OWAttackTileTypeEnum.ATTACKABLE))},
				new OWAttackTile[] {OWAttackTile.create(map.getTile(x+1, y-2), 
						OWAttackTile.create(map.getTile(x, y-2), OWAttackTileTypeEnum.ATTACKABLE), OWAttackTile.create(map.getTile(x+2, y-2), OWAttackTileTypeEnum.ATTACKABLE))},
				//Upper right
				new OWAttackTile[] {OWAttackTile.create(map.getTile(x+2, y-2), 
						OWAttackTile.create(map.getTile(x+1, y-2), OWAttackTileTypeEnum.ATTACKABLE), OWAttackTile.create(map.getTile(x+2, y-1), OWAttackTileTypeEnum.ATTACKABLE))},
				//Right middle
				new OWAttackTile[] {OWAttackTile.create(map.getTile(x+2, y-1), 
						OWAttackTile.create(map.getTile(x+2, y-2), OWAttackTileTypeEnum.ATTACKABLE), OWAttackTile.create(map.getTile(x+2, y), OWAttackTileTypeEnum.ATTACKABLE))}, 
				new OWAttackTile[] {OWAttackTile.create(map.getTile(x+2, y), 
						OWAttackTile.create(map.getTile(x+2, y-1), OWAttackTileTypeEnum.ATTACKABLE), OWAttackTile.create(map.getTile(x+2, y+1), OWAttackTileTypeEnum.ATTACKABLE))}, 
				new OWAttackTile[] {OWAttackTile.create(map.getTile(x+2, y+1), 
						OWAttackTile.create(map.getTile(x+2, y), OWAttackTileTypeEnum.ATTACKABLE), OWAttackTile.create(map.getTile(x+2, y+2), OWAttackTileTypeEnum.ATTACKABLE))},
				//Bottom right
				new OWAttackTile[] {OWAttackTile.create(map.getTile(x+2, y+2), 
						OWAttackTile.create(map.getTile(x+2, y+1), OWAttackTileTypeEnum.ATTACKABLE), OWAttackTile.create(map.getTile(x+1, y+2), OWAttackTileTypeEnum.ATTACKABLE))},
				//Bottom middle
				new OWAttackTile[] {OWAttackTile.create(map.getTile(x+1, y+2), 
						OWAttackTile.create(map.getTile(x+2, y+2), OWAttackTileTypeEnum.ATTACKABLE), OWAttackTile.create(map.getTile(x, y+2), OWAttackTileTypeEnum.ATTACKABLE))},
				new OWAttackTile[] {OWAttackTile.create(map.getTile(x, y+2), 
						OWAttackTile.create(map.getTile(x+1, y+2), OWAttackTileTypeEnum.ATTACKABLE), OWAttackTile.create(map.getTile(x-1, y+2), OWAttackTileTypeEnum.ATTACKABLE))},
				new OWAttackTile[] {OWAttackTile.create(map.getTile(x-1, y+2), 
						OWAttackTile.create(map.getTile(x, y+2), OWAttackTileTypeEnum.ATTACKABLE), OWAttackTile.create(map.getTile(x-2, y+2), OWAttackTileTypeEnum.ATTACKABLE))},
				//Bottom left
				new OWAttackTile[] {OWAttackTile.create(map.getTile(x-2, y+2), 
						OWAttackTile.create(map.getTile(x-1, y+2), OWAttackTileTypeEnum.ATTACKABLE), OWAttackTile.create(map.getTile(x-2, y+1), OWAttackTileTypeEnum.ATTACKABLE))}, 
				//Left middle
				new OWAttackTile[] {OWAttackTile.create(map.getTile(x-2, y+1), 
						OWAttackTile.create(map.getTile(x-2, y+2), OWAttackTileTypeEnum.ATTACKABLE), OWAttackTile.create(map.getTile(x-2, y), OWAttackTileTypeEnum.ATTACKABLE))},
				new OWAttackTile[] {OWAttackTile.create(map.getTile(x-2, y), 
						OWAttackTile.create(map.getTile(x-2, y+1), OWAttackTileTypeEnum.ATTACKABLE), OWAttackTile.create(map.getTile(x-2, y-1), OWAttackTileTypeEnum.ATTACKABLE))}, 
				new OWAttackTile[] {OWAttackTile.create(map.getTile(x-2, y-1), 
						OWAttackTile.create(map.getTile(x-2, y), OWAttackTileTypeEnum.ATTACKABLE), OWAttackTile.create(map.getTile(x-2, y-2), OWAttackTileTypeEnum.ATTACKABLE))},			
				
				
				 };

		return attackTiles;
	}
}
